//
//  LBWWelcomeViewController.swift
//  weibo
//
//  Created by Limonwei on 15/12/8.
//  Copyright © 2015年 heima. All rights reserved.
//

//欢迎视图

import UIKit
import SnapKit
import SDWebImage

class LBWWelcomeViewController: UIViewController {

    
    override func loadView() {
        view = bgImageView
        
        setupUI()
    }
    
    private func setupUI(){
        
        //添加控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        //添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(view.snp_top).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(15)
        }
        
    }
    
    //视图加载完后会出现一个跳动的动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //先更改约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        
        //执行动画
        messageLabel.alpha = 0
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        //欢迎页面跳转到主页
                        //发送一个切换根控制器的通知
                        NSNotificationCenter.defaultCenter().postNotificationName(LBWSwitchRootVCNotifation, object: nil)
                })
        }
    }

   
    //懒加载控件
    //背景图片
    private lazy var bgImageView: UIImageView = {
        
        let image = UIImage(named: "ad_background")
        
        return UIImageView(image: image)
        
    }()
    
    //头像
    private lazy var headImageView: UIImageView = {
       
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
       
        let user = LBWUserAccountViewModel.shareAccount.userAccount!
        let avatar = user.avatar_large!
        
        let url = NSURL(string:  user.avatar_large!)!
        
        //异步下载图片
        imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        
        //切圆角
        imageView.cornerRadius = 45
        
        return imageView
        
    }()
    
    //欢迎回来label
    private lazy var messageLabel: UILabel = {
       
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(18)
        
        label.text = "欢迎回来"
        
        label.tintColor = UIColor.darkGrayColor()
        
        return label
        
    }()
    

  

}






























